using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.Rails.Data;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_sets : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();
	private Account acc = new Account();
	private Account user = new Account();
	private bool isLoggedIn;
	private ServerPath serverpath = new ServerPath();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		string usr = "";
		bool canCreate = false;

		isLoggedIn = false;

		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			acc = ap.GetAccountBySession(Session["Zappr"].ToString());
			isLoggedIn = true;
		}

		if(Request.QueryString.Get("user") != null) {
			usr = Request.QueryString.Get("user").ToString();

			user = this.ap.GetAccountByUsername(usr);

			lblSetsTitle.Text = usr + "'s ";
			lblSetsTitle.Text += GetLocalResourceObject("Sets").ToString();

			plcHead.Controls.Add(new LiteralControl("<br />"));
			plcHead.Controls.Add(new LiteralControl("<br />"));

			HyperLink usrimg = new HyperLink();
			usrimg.CssClass = "normal";
			//usrimg.NavigateUrl = "~/default.aspx?id=images&user=" + usr;
			usrimg.NavigateUrl = "~/images/" + usr + "/default.aspx";
			usrimg.Text = usr + "'s ";
			usrimg.Text += GetLocalResourceObject("Photos").ToString();

			plcHead.Controls.Add(usrimg);

			// check set amount
			//int amount = ip.GetUserSetsAmount(user.ID);

			plcHead.Controls.Add(new LiteralControl("<span class=\"txtNormal\">|</span>"));

			HyperLink news = new HyperLink();
			news.CssClass = "normal";
			news.NavigateUrl = "~/default.aspx?id=_createset";
			news.Text = GetLocalResourceObject("CreateSet").ToString();

			plcHead.Controls.Add(news);

			this.LoadSets();
		}
	}

	/// <summary>
	/// Load sets
	/// </summary>
	private void LoadSets() {
		Account puser = ap.GetAccountByUsername(Request.QueryString.Get("user").ToString());
		int line = 0;
		int amount = 0;

		List<SetItem> list = ip.GetUserSets(
			puser.ID,
			false
		);

		if(list != null) {
			if(list.Count > 0) {
				TableRow row = null;

				foreach(SetItem set in list) {
					if(line == 0) {
						row = new TableRow();
					}

					TableCell cell = new TableCell();
					cell.VerticalAlign = VerticalAlign.Top;
					cell.HorizontalAlign = HorizontalAlign.Justify;
					cell.Width = new Unit(128);
					cell.Style.Add("width", "128px");

					// thumbnail
					Panel pimg = new Panel();
					pimg.CssClass = "SetCase";

					if(set.Thumbnail != -1) {
						ImageItem mainimg = ip.GetImage(Convert.ToInt64(set.Thumbnail));
						
						HyperLink thumb = new HyperLink();

						thumb.NavigateUrl = "~/default.aspx?set=" + set.SetID.ToString();
						thumb.ImageUrl = "~/Images/" + mainimg.Image75x75;
						thumb.Style.Add("border", "1px solid #eee");
						thumb.Style.Add("position", "absolute");
						thumb.Style.Add("margin-left", "7px");

						pimg.Controls.Add(thumb);
					}

					cell.Controls.Add(pimg);

					// name
					HyperLink slink = new HyperLink();
					slink.CssClass = "main";
					//slink.NavigateUrl = "~/default.aspx?set=" + set.SetID.ToString();
					slink.NavigateUrl = "~/set/" + set.SetID.ToString() + "/default.aspx";
					slink.Text = set.Name;
					slink.Style.Add("text-align", "left");
					slink.Style.Add("margin-left", "7px");

					cell.Controls.Add(slink);

					// amount
					amount = ip.GetSetAmount(set.ID, puser.ID);

					Label lblamount = new Label();
					lblamount.CssClass = "listFooter";
					lblamount.Text = " " + amount.ToString() + " ";
					lblamount.Text += GetLocalResourceObject("Photos").ToString();
					lblamount.Style.Add("text-align", "left");
					lblamount.Style.Add("margin-left", "7px");
					lblamount.Style.Add("padding-top", "4px");

					cell.Controls.Add(new LiteralControl("<br />"));
					cell.Controls.Add(lblamount);

					// ---------------------

					if(isLoggedIn && puser.ID == acc.ID) {
						// change link
						HyperLink change = new HyperLink();
						change.CssClass = "listFooter";
						change.Text = GetLocalResourceObject("Change").ToString();
						change.NavigateUrl = "~/default.aspx?id=_editset&set=" + set.SetID.ToString();
						change.Style.Add("text-align", "left");
						change.Style.Add("margin-left", "7px");

						cell.Controls.Add(new LiteralControl("<br />"));
						cell.Controls.Add(new LiteralControl("<br />"));
						cell.Controls.Add(change);

						// delete
						LinkButton del = new LinkButton();
						del.CssClass = "listFooter";
						del.Text = GetLocalResourceObject("Delete").ToString();
						del.OnClientClick = "javascript:chk=confirm('"
							+ GetLocalResourceObject("DeleteThis").ToString()
							+ "');return chk;";
						del.Command += new CommandEventHandler(btnDelete_Click);
						del.CommandName = "btnDelete_Click";
						del.CommandArgument = set.ID + "$" + acc.Username;
						del.Style.Add("text-align", "left");
						del.Style.Add("margin-left", "7px");

						cell.Controls.Add(new LiteralControl("<br />"));
						cell.Controls.Add(del);
					}

					row.Cells.Add(cell);
					plcSet.Rows.Add(row);

					line++;

					if(line == 6) {
						row = null;
						line = 0;
					}
				}
			}
		}
	}

	/// <summary>
	/// btnDelete_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnDelete_Click(object sender, CommandEventArgs e) {
		char[] seperator = new char[1];
		seperator[0] = '$';
		string[] spliter = e.CommandArgument.ToString().Split(seperator);

		ip.DeleteSet(spliter[0]);

		//Response.Redirect("default.aspx?id=images&user=" + spliter[0]);
		Response.Redirect("~/images/" + spliter[0] + "/default.aspx");
	}
}
